<script>

// import { checkIdCard } from '@/utils/idcard'

export default {
  name: 'VipAuth',
  data () {
    return {
      show: false,
      formData: {
        idcard: '',
        idcard1: '',
        idcard2: ''
      },
      rules: {
        idcard: [
          { required: true, message: '请输入身份证号' }
          // {
          //   validator: (rule, value, callback) => {
          //     if (checkIdCard(value)) {
          //       callback()
          //     } else {
          //       callback(new Error('身份证号格式错误'))
          //     }
          //   }
          // }
        ],
        idcard1: [
          { required: true, message: '请上传身份证正面' }
        ],
        idcard2: [
          { required: true, message: '请上传身份证反面' }
        ]
      },
      uploadUrl: '',
      headers: {},
      idcard1Url: '',
      idcard2Url: '',
      basePath: '',
      active: 0
    }
  },
  methods: {
    init (id) {
      this.show = true
      if (id) {
        this.$axios.get(`vip/exam/info/${id}`).then(data => {
          if (data.userinfoStatus === 1 || data.userinfoStatus === 4) {
            this.active = 0
          } else if (data.userinfoStatus === 2) {
            this.active = 1
          } else {
            this.active = 2
          }
          this.formData = { ...data }
          this.idcard1Url = this.basePath + data.idcard1
          this.idcard2Url = this.basePath + data.idcard2
        })
      }
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt20M = file.size / 1024 / 1024 < 20

      if (!isImage) {
        this.$message.error('上传图片只能是 JPG或PNG 格式!')
      }
      if (!isLt20M) {
        this.$message.error('上传图片大小不能超过 20MB!')
      }
      return isImage && isLt20M
    },
    idcard1Success (res) {
      if (res.code === 200) {
        this.idcard1Url = this.basePath + res.data.filename
        this.formData.idcard1 = res.data.filename
      } else {
        this.$message.error(res.msg)
      }
    },
    idcard2Success (res) {
      if (res.code === 200) {
        this.idcard2Url = this.basePath + res.data.filename
        this.formData.idcard2 = res.data.filename
      } else {
        this.$message.error(res.msg)
      }
    },
    submit () {
      this.$refs.authForm.validate(valid => {
        if (valid) {
          this.$axios.post('vip/auth', this.formData).then(() => {
            this.$message.success('提交成功，请耐心等待管理员审核！')
            this.show = false
            this.$emit('close')
          }).catch(e => e)
        }
      })
    }
  },
  created () {
    this.basePath = process.env.VUE_APP_BASE_PATH
    this.uploadUrl = this.basePath + 'upload'
    this.headers = {
      token: this.$cookies.get('token')
    }
  }
}
</script>

<template>
<el-dialog title="实名认证" :visible.sync="show">
  <el-steps :active="active" process-status="success" style="width: 80%;margin: 10px auto">
    <el-step title="未认证"></el-step>
    <el-step title="已提交"></el-step>
    <el-step title="已认证"></el-step>
  </el-steps>
  <el-form ref="authForm" :model="formData" :rules="rules" label-width="100px">
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="formData.idcard" :disabled="active>0" placeholder="请输入身份证号"/>
    </el-form-item>
    <el-form-item label="身份证正面" prop="idcard1">
      <el-upload
        :disabled="active>0"
        class="avatar-uploader"
        :action="uploadUrl"
        :headers="headers"
        :data="{type:'idcard'}"
        :show-file-list="false"
        :on-success="idcard1Success"
        :before-upload="beforeUpload">
        <img v-if="idcard1Url" :src="idcard1Url" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="身份证反面" prop="idcard2">
      <el-upload
        :disabled="active>0"
        class="avatar-uploader"
        :action="uploadUrl"
        :headers="headers"
        :data="{type:'idcard'}"
        :show-file-list="false"
        :on-success="idcard2Success"
        :before-upload="beforeUpload">
        <img v-if="idcard2Url" :src="idcard2Url" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item v-if="active === 0">
      <el-button @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped lang="scss">

</style>
